<?php

namespace component\system\system_user_login;

use common\logic\Logic;
use Jenssegers\Agent\Agent;


class SystemUserLogin extends Logic
{


    /**
     * @var array
     */
    public $fields = [
        'id',
        'user_id',
        'ip',
        'agent',
        'languages',
        'device',
        'browser',
        'version_browser',
        'platform',
        'version_platform',
        'phone',
        'robot',
        'create_time',
    ];

    /**
     * @return SystemUserLoginModel|\think\Model
     */
    protected function model()
    {
        return new SystemUserLoginModel();
    }

    /**
     * @param $user_id
     * @return array|bool|null
     */
    public function getListByUserId($user_id)
    {
        if (empty($user_id)) {
            return false;
        }
        return $this->select(['user_id' => $user_id]);
    }

    /**
     * 写入日志
     * @param $user_id
     * @return bool
     */
    public function create($user_id)
    {
        $agent = new Agent();
        $model = $this->model();
        $model->user_id = $user_id;
        $model->languages = $agent->languages();
        $model->agent = $agent->getUserAgent();
        $model->ip = request()->ip();
        $model->browser = $agent->browser();
        $model->version_browser = $agent->version($agent->browser());
        $model->platform = $agent->platform();
        $model->version_platform = $agent->version($agent->platform());
        $model->phone = $agent->isPhone();
        $model->robot = $agent->robot();
        return $model->save();
    }
}